#include "MoveAction.hpp"
#include "Chessboard.hpp"
#include <cassert>
#include "Exception.hpp"

MoveAction::MoveAction(Chess::Color color, const Position& pos1, const Position& pos2)
	:	Action(color, Action::MOVE), pos1(pos1), pos2(pos2), piece_captured(0)
{}

MoveAction::MoveAction(const MoveAction& maction)
	: Action(maction), pos1(maction.pos1), pos2(maction.pos2), piece_captured(0)
{}

MoveAction::~MoveAction()
{}

void MoveAction::execute()
{
	try
	{
		Cell*			src = 0;
		Cell*			dst = 0;
		Piece*			piece = 0;
		
		piece_captured = 0;
		src = Chess::getInstance()->getChessboard()->getCell(pos1);
		dst = Chess::getInstance()->getChessboard()->getCell(pos2);
		
		assert ((piece = src->getPiece()) != 0);
		
		if (dst->hasPiece())
		{
			piece_captured = dst->getPiece();
			dst->setPiece(0);
			piece_captured->setCell(0);
		}
		
		piece->setCell(dst);
		dst->setPiece(piece);
		src->setPiece(0);
		
		piece->incrNbMovements();
	}
	catch(Exception* e)
	{	throw e;	}
}

const Position&	MoveAction::getPos1() const
{	return (pos1);		}

const Position& MoveAction::getPos2() const
{	return (pos2);		}

Piece* MoveAction::getPieceCaptured() const
{	return (piece_captured);	}

void	MoveAction::displayDbg()
{
	/*
	cout << "MOVE ACTION " << hex << this;
	cout << "	COLOR " << string((color == Chess::WHITE) ? "white" : "black");
	cout << "	FROM " << dec << pos1.getRealX() << ":" << dec << pos1.getRealY();
	cout << "	TO " << dec << pos2.getRealX() << ":" << dec << pos2.getRealY() << endl;*/
}
